/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
$(function() {
    if (typeof(Storage) !== "undefined")
    {
       
        if (sessionStorage.check_get_info_setting == 1) {
            //signup1
            if (sessionStorage.setting_fn) {
                if ($('input[name="firstname"]')[0]) {
                    $('input[name="firstname"]').val(sessionStorage.setting_fn);
                }
            }
            if (sessionStorage.setting_ln) {
                if ($('input[name="lastname"]')[0]) {
                    $('input[name="lastname"]').val(sessionStorage.setting_ln);
                }
            }

            if (sessionStorage.setting_pass) {
                if ($('input[name="password"]')[0]) {
                    if (sessionStorage.setting_pass !== "") {
                        $('input[name="password"]').prop('type', 'password');
                        $('input[name="password"]').val(sessionStorage.setting_pass);
                        $('input[name="password_2"]').prop('type', 'password');
                        $('input[name="password_2"]').val(sessionStorage.setting_pass);
                    }
                }
            }
        }
        if (sessionStorage.check_get_info_setting_done == 1) {
            //signup2
            if (sessionStorage.setting_fiance_fn) {
                if ($('input[name="fiance_firstname"]')[0]) {
                    $('input[name="fiance_firstname"]').val(sessionStorage.setting_fiance_fn);
                }
            }
            if (sessionStorage.setting_fiance_ln) {
                if ($('input[name="fiance_lastname"]')[0]) {
                    $('input[name="fiance_lastname"]').val(sessionStorage.setting_fiance_ln);
                }
            }
            if (sessionStorage.setting_fiance_date) {
                if ($('input[name="fiance_date"]')[0]) {
                    $('input[name="fiance_date"]').val(sessionStorage.setting_fiance_date);
                }
            }
        }



        $(".next-page").click(function() {
            var validate = checkValidate('error-msg');
            if (validate) {
                if (typeof(Storage) !== "undefined")
                {
                    // Yes! localStorage and sessionStorage support!
                    // Some code.....
                    sessionStorage.setting_fn = $('input[name="firstname"]').val();
                    sessionStorage.setting_ln = $('input[name="lastname"]').val();
                    if ($('input[name="password"]').val() !== '' && $('input[name="password"]').val() !== 'Password') {
                        sessionStorage.setting_pass = $('input[name="password"]').val();
                    } else {
                        sessionStorage.setting_pass = '';
                    }
                    wd_redirect("settingDone.html");
                }
                else
                {
                    print_alert("Sorry! web app storage session was not supported!");
                    // Sorry! No web storage support..
                }

            }
        });
        $(".back-page").click(function() {

            if (typeof(Storage) !== "undefined")
            {
                // Yes! localStorage and sessionStorage support!
                // Some code.....
                sessionStorage.setting_fiance_fn = $('input[name="fiance_firstname"]').val();
                sessionStorage.setting_fiance_ln = $('input[name="fiance_lastname"]').val();
                sessionStorage.setting_fiance_date = $('input[name="fiance_date"]').val();
                wd_redirect("setting.html");
            }
            else
            {
                print_alert("Sorry! web app storage session was not supported!");
                // Sorry! No web storage support..
            }


        });

        $(".btn_save_profile").click(function() {

            if (checkValidate_signup_2('error-msg')) {
                $(".btn_save_profile").attr("disabled", "disabled");
                if (sessionStorage.setting_pass === '' || sessionStorage.setting_pass === 'Password') {
                    sessionStorage.setting_pass = '';
                }
                $.post(localStorage.domain_app+"/webservice/updateUser.php", {
                    firstName: sessionStorage.setting_fn,
                    lastName: sessionStorage.setting_ln,
                    password: sessionStorage.setting_pass,
                    fianceFirstName: $('input[name="fiance_firstname"]').val(),
                    fianceLastName: $('input[name="fiance_lastname"]').val(),
                    weddingDate: $('input[name="fiance_date"]').val(),
                    user_id: localStorage.userID,
                    email: localStorage.email
                }, function(res) {
                    var num_milliseconds = 3000;
                    if (res.error === 0) {
                        sessionStorage.check_get_info_setting_done = 0;
                        sessionStorage.check_get_info_setting = 0;
                        print_alert(res.msg);
                        localStorage.firstName = res.firstname;
                        localStorage.lastName = res.lastname;
                        wd_redirect("setting.html");
                    } else {
                        $('#error-msg').addClass('active').children().html(res.msg);
                        hideError_2('error-msg', num_milliseconds);

                    }
                    return false;

                    $(".btn_save_profile").removeAttr("disabled");
                }, "json");
            }
        });
    }
    $('.profile-password').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value !== '' && this.value !== "Password" && this.value !== "Re-password") {
            
            this.select();
        } else {
            $(this).val('').prop('type', 'password');
        }
    })
            .blur(function() {
        if ($(this).val() == '' || $(this).val() == 'Password' || $(this).val() == 'Re-password') {
            console.log($(this).attr('rel'));
            $(this).val($(this).attr('rel')).prop('type', 'text');
           
        }
    });
    $('input[type="text"]').focus(function() {
       $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            if ($(this).hasClass("fn")) {
                $(this).val(sessionStorage.setting_fn);
            } else {
                $(this).val(sessionStorage.setting_ln);
            }
            
        }
    });
    $('input[type="date"]').focus(function() {
        $(this).css("color", "#9A7E72");
        if (this.value == this.defaultValue) {
            $(this).val("");

        } else if (this.value != this.defaultValue) {
            
            this.select();
        }

    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');
           
        }
    });


});
function checkValidate_signup_2(id_append) {
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    if ($('input[name="fiance_firstname"]').val() == '' || $('input[name="fiance_firstname"]').val() == 'Fiance’s First Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="fiance_firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s first name cannot include special characters");
            hideError(id_append, $('input[name="fiance_firstname"]'), num_milliseconds);
            return false;
        } 

    }

    if ($('input[name="fiance_lastname"]').val() == '' || $('input[name="fiance_lastname"]').val() == 'Fiance’s Last Name') {
        msgEr = errormsg[2].replace('%s', 'fiance’s last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="fiance_lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Fiance’s last name cannot include special characters");
            hideError(id_append, $('input[name="fiance_lastname"]'), num_milliseconds);
            return false;
        } 
    }
    if ($('input[name="fiance_date"]').val() == '' || $('input[name="fiance_date"]').val() == 'Wedding Date (mm/dd/yy)') {
        msgEr = errormsg[2].replace('%s', 'wedding date');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
        return false;
    }
    else {
        var rxDatePattern = /(19|20)\d\d[- \/.](0?[1-9]|1[012])[- \/.](0?[1-9]|[12][0-9]|3[01])/;
        var date = $('input[name="fiance_date"]').val();
        if (!date.match(rxDatePattern)) {
            msgEr = errormsg[8].replace('%s', 'wedding date');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="fiance_date"]'), num_milliseconds);
            return false;
        }
    }

    return true;
}
function checkValidate(id_append) {
    var check_char = /^[a-zA-Z\s]+$/;
    // error msg depends on the language
    var errormsg = new Array();
    errormsg[1] = 'The %s field is required.';
    errormsg[2] = 'Please input your %s';
    errormsg[3] = 'The %s field must contain only numbers.';
    errormsg[4] = 'This %s address is invalid.';
    errormsg[5] = '%s is too short. %s2 must be at least %number characters';
    errormsg[6] = 'Confirm password does not match password';
    errormsg[7] = 'The %s field must not exceed %number characters in length.';
    errormsg[8] = 'This %s is invalid.';
    //show msg
    var msgEr = '';
    var valueStr = '';
    var num_milliseconds = 3000;
    //check
    if ($('input[name="firstname"]').val() == '' || $('input[name="firstname"]').val() == 'First Name') {
        msgEr = errormsg[2].replace('%s', 'first name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
        return false;
    } else {
        valueStr = $('input[name="firstname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("First name cannot include special characters");
            hideError(id_append, $('input[name="firstname"]'), num_milliseconds);
            return false;
        }

    }

    if ($('input[name="lastname"]').val() == '' || $('input[name="lastname"]').val() == 'Last Name') {
        msgEr = errormsg[2].replace('%s', 'last name');
        $('#' + id_append + '').addClass('active').children().html(msgEr);
        hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
        return false;
    }
    else {
        valueStr = $('input[name="lastname"]').val();
        if (!check_char.test(valueStr)) {
            $('#' + id_append + '').addClass('active').children().html("Last name cannot include special characters");
            hideError(id_append, $('input[name="lastname"]'), num_milliseconds);
            return false;
        } 

    }
    var valueStr = $('input[name="password"]').val();
    var valueStr2 = $('input[name="password_2"]').val();
    if ((valueStr !== '' && valueStr !== 'Password') || (valueStr2 !== '' && valueStr2 !== 'Re-password')) {
        if (!isLengthBetween(valueStr, 6)) {
            msgEr = errormsg[5].replace('%s', 'Password').replace('%s2', 'Password').replace('%number', '6');
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="password"]'), num_milliseconds);
            return false;
        } else if (valueStr !== valueStr2) {
            msgEr = errormsg[6];
            $('#' + id_append + '').addClass('active').children().html(msgEr);
            hideError(id_append, $('input[name="password_2"]'), num_milliseconds);
            return false;
        }
    } 

    return true;
}
function hideError(id_append, item, num_milliseconds) {
    item.css("color", "red");
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function hideError_2(id_append, num_milliseconds) {
    setTimeout(function() {
        $('#' + id_append + '').fadeOut('fast', function() {
        });
        $('#' + id_append + '').removeClass('active').children().html('');
    }, num_milliseconds);
}
function isLengthBetween(str, min, max) {
    if (max) {
        return (str.length >= min) && (str.length <= max);
    } else {
        return (str.length >= min);
    }
}